﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.AuditLogging.ContingencyService.Configuration;
using System.Configuration;
using PSE.Framework.Configuration;

namespace PSE.AuditLogging.ContingencyService
{
    /// <summary>
    /// Classe que encapsula a leitura das configurações utilizadas pelo client
    /// </summary>
    internal class ConfigurationReader
    {
        static object _syncRoot = new object();
        static LogResenderConfigurationSection _contingencyServiceConfiguration;

        public static LogResenderConfigurationSection ContingencyServiceConfiguration
        {
            get
            {
                lock (_syncRoot)
                {
                    if (_contingencyServiceConfiguration == null)
                        _contingencyServiceConfiguration = System.Configuration.ConfigurationManager.GetSection(Constants.LOGRESENDER_SECTION_NAME) as LogResenderConfigurationSection;

                    if (_contingencyServiceConfiguration == null)
                        throw new ConfigurationErrorsException(string.Format("Configuration section '{0}' not found in config file.", Constants.LOGRESENDER_SECTION_NAME));

                    return _contingencyServiceConfiguration;
                }
            }
        }


        public static void Refresh()
        {
            lock (_syncRoot)
            {
                //tentar recuperar a configuracao do arquivo de config do executavel
                System.Configuration.ConfigurationManager.RefreshSection(Constants.LOGRESENDER_SECTION_NAME);
                _contingencyServiceConfiguration = null;
            }
        }
    }
}
